package admin

import (
	"encoding/json"
	"fmt"
	"go-cmdb/controllers"
	"go-cmdb/models"
)

type WebSiteController struct {
	AdminController
}

func NewWebSiteController() *WebSiteController {
	ctrl := &WebSiteController{}

	return ctrl
}

func (ctrl *WebSiteController) Site() {
	tmpl := ctrl.GetString(":tmpl")
	code := ctrl.GetString(":code")
	m, _ := models.NewWebSite().GetByUUID(code)
	if m.Status == 2 {
		ctrl.ErrorView(controllers.ErrorMsg{
			Msg:  "已被禁用",
			Code: 500,
		})
		return
	}

	if m.Tmpl != tmpl {
		ctrl.ErrorView(controllers.ErrorMsg{
			Msg:  "类型不匹配",
			Code: 500,
		})
		return
	}

	ctrl.ViewTplData(fmt.Sprintf("%s-tmpl", tmpl), map[string]interface{}{
		"title":     m.Name,
		"site":      m.ToJson(),
		"view_type": "preview",
	})
	return
}
func (ctrl *WebSiteController) Prepare() {

	ctrl.AdminController.Prepare()
	ctrl.IgnoreLayout(func(act string) bool {
		return act == "Design" || act == "Site"
	})

}

func (ctrl *WebSiteController) Index() {

	ctrl.ViewData(map[string]interface{}{
		"title": "网页设计",
	})
	return

}

func (ctrl *WebSiteController) Design() {
	tmplType := ctrl.GetString("tmpl")
	id, _ := ctrl.GetUint64("id")
	m, _ := models.NewWebSite().Get(id)
	if m.Tmpl != tmplType {
		ctrl.ErrorView(controllers.ErrorMsg{
			Msg:  "类型不匹配",
			Code: 500,
		})
		return
	}

	ctrl.ViewTplData(fmt.Sprintf("%s-tmpl", tmplType), map[string]interface{}{
		"site":      m.ToJson(),
		"title":     m.Name,
		"view_type": "design",
	})
	return
}

func (ctrl *WebSiteController) Page() {

	var (
		pageSize, _  = ctrl.GetUint64("size", 0)
		pageIndex, _ = ctrl.GetUint64("page", 0)
	)

	lst, count, _ := models.NewWebSite().GetByPage(pageSize, pageIndex)

	ctrl.OK(map[string]interface{}{
		"rows":  lst,
		"total": count,
	})
	return

}
func (ctrl *WebSiteController) Add() {

	var req models.AddWebSiteReq

	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	curUser, _ := ctrl.GetAuthUser()
	if err := models.NewWebSite().Add(req, curUser.UserID); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	ctrl.OK(nil)
	return

}

func (ctrl *WebSiteController) Find() {

	id, _ := ctrl.GetUint64("id", 0)
	m, _ := models.NewWebSite().Get(id)
	ctrl.OK(m)
	return

}

func (ctrl *WebSiteController) Remove() {

	var req models.RemoveWebSiteReq

	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	curUser, _ := ctrl.GetAuthUser()
	if err := models.NewWebSite().CStatus(0, req.ID, curUser.UserID); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	ctrl.OK(nil)
	return

}
func (ctrl *WebSiteController) Enable() {

	var req models.EnableWebSiteReq

	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	curUser, _ := ctrl.GetAuthUser()
	if err := models.NewWebSite().CStatus(req.Status, req.ID, curUser.UserID); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	ctrl.OK(nil)
	return

}

func (ctrl *WebSiteController) Update() {

	var req models.UpdateWebSiteReq

	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	curUser, _ := ctrl.GetAuthUser()
	if err := models.NewWebSite().Update(req.Name, req.ID, curUser.UserID); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	ctrl.OK(nil)
	return

}

func (ctrl *WebSiteController) SaveConfig() {

	var req models.SaveWebSiteReq

	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}

	curUser, _ := ctrl.GetAuthUser()
	if err := models.NewWebSite().SaveWebSiteConfig(req.Config, req.ID, curUser.UserID); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	ctrl.OK(nil)
	return

}
